﻿Imports System.Runtime.InteropServices

Namespace OpenCL
    ''' <summary>
    ''' OpenCLのAPI関数集合クラス
    ''' </summary>
    ''' <remarks>
    ''' <seealso>http://opencl-percipere.blogspot.jp/</seealso>
    ''' <seealso>http://memeplex.blog.shinobi.jp/opencl/</seealso>
    ''' <seealso>http://wiki.livedoor.jp/mikk_ni3_92/</seealso>
    ''' </remarks>
    Public NotInheritable Class OpenCLAPI

        Private Sub New()
        End Sub

#Region "　プラットフォーム　"
        ''' <summary>
        ''' プラットフォームの一覧を取得
        ''' </summary>
        ''' <param name="entryCount">システムに検索させる Platform 数の最大値</param>
        ''' <param name="platforms">検索により取得した Platform のハンドル</param>
        ''' <param name="platformCount">取得したPlatform ハンドルの個数 (entryCountを上限とする)</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("OpenCL.dll")> _
        Public Shared Function clGetPlatformIDs(ByVal entryCount As Integer, ByVal platforms As IntPtr(), ByRef platformCount As Integer) As Integer
        End Function
        ''' <summary>
        ''' プラットフォームの情報を取得
        ''' </summary>
        ''' <param name="platform">情報取得するPlatform ハンドル(単独)</param>
        ''' <param name="parameterName">入手する情報を指定するためのパラメータ</param>
        ''' <param name="parameterValueSize">情報入手用のバッファーのサイズ</param>
        ''' <param name="parameterValue">情報入手用のバッファー</param>
        ''' <param name="parameterValueSizeReturn">実際に取得した情報のサイズ</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("OpenCL.dll")> _
        Public Shared Function clGetPlatformInfo(ByVal platform As IntPtr, ByVal parameterName As PlatformInfo, ByVal parameterValueSize As Integer, ByVal parameterValue As System.Text.StringBuilder, ByRef parameterValueSizeReturn As Integer) As Integer
        End Function
#End Region

#Region "　デバイス　"
        ''' <summary>
        ''' デバイスの一覧を取得
        ''' </summary>
        ''' <param name="platform">情報取得対象の Platform ハンドル(単独)</param>
        ''' <param name="deviceType">取得するデバイスの種類</param>
        ''' <param name="entryCount">取得するデバイス数の上限</param>
        ''' <param name="devices">デバイスのハンドル(複数)</param>
        ''' <param name="deviceCount">デバイスハンドル数</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("OpenCL.dll")> _
        Public Shared Function clGetDeviceIDs(ByVal platform As IntPtr, ByVal deviceType As DeviceType, ByVal entryCount As Integer, ByVal devices As IntPtr(), ByRef deviceCount As Integer) As Integer
        End Function
        ''' <summary>
        ''' デバイス固有情報の取得
        ''' </summary>
        ''' <param name="device">情報取得対象の Device ハンドル(単独)</param>
        ''' <param name="paramName">入手する情報を指定するためのパラメータ</param>
        ''' <param name="paramValueSize">情報入手用のバッファーのサイズ</param>
        ''' <param name="paramValue">情報入手用のバッファー</param>
        ''' <param name="paramValueSizeReturn">実際に取得した情報のサイズ</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("OpenCL.dll")> _
        Public Shared Function clGetDeviceInfo(ByVal device As IntPtr, ByVal paramName As DeviceInfoString, ByVal paramValueSize As Integer, ByVal paramValue As System.Text.StringBuilder, ByRef paramValueSizeReturn As Integer) As Integer
        End Function
#End Region


        <DllImport("OpenCL.dll")> _
        Public Shared Function clCreateContext(ByVal properties As IntPtr(), ByVal deviceCount As Integer, ByVal devices As IntPtr(), ByVal pfnNotify As NotifyCallback, ByVal userData As IntPtr, ByRef errorCode As Integer) As IntPtr
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clReleaseContext(ByVal context As IntPtr) As Integer
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clCreateCommandQueue(ByVal context As IntPtr, ByVal device As IntPtr, ByVal properties As Long, ByRef errorCodeReturn As Integer) As IntPtr
        End Function
        <DllImport("OpenCL.dll")> _
        Public Shared Function clReleaseCommandQueue(ByVal commandQueue As IntPtr) As Integer
        End Function

#Region "　バッファ　"
        <DllImport("OpenCL.dll")> _
        Public Shared Function clCreateBuffer(ByVal context As IntPtr, ByVal allocationAndUsage As MemoryFlags, ByVal sizeInBytes As Integer, ByVal hostPtr As IntPtr, ByRef errorCodeReturn As Integer) As IntPtr
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clReleaseMemObject(ByVal memoryObject As IntPtr) As Integer
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clEnqueueReadBuffer(ByVal commandQueue As IntPtr, ByVal buffer As IntPtr, ByVal isBlocking As Boolean, ByVal offset As Integer, ByVal sizeInBytes As Integer, ByVal result As IntPtr, _
         ByVal numberOfEventsInWaitList As Integer, ByVal eventWaitList As IntPtr, ByVal eventObjectOut As IntPtr) As Integer
        End Function
#End Region

#Region "　プログラム　"
        <DllImport("OpenCL.dll")> _
        Public Shared Function clCreateProgramWithSource(ByVal context As IntPtr, ByVal count As Integer, ByVal programSources As String(), ByVal sourceLengths As Integer(), ByRef errorCode As Integer) As IntPtr
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clBuildProgram(ByVal program As IntPtr, ByVal deviceCount As Integer, ByVal deviceList As IntPtr(), ByVal buildOptions As String, ByVal notify As NotifyProgramBuilt, ByVal userData As IntPtr) As Integer
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clReleaseProgram(ByVal program As IntPtr) As Integer
        End Function
#End Region

#Region "　カーネル　"
        <DllImport("OpenCL.dll")> _
        Public Shared Function clCreateKernel(ByVal kernel As IntPtr, ByVal functionName As String, ByRef errorCode As Integer) As IntPtr
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clReleaseKernel(ByVal kernel As IntPtr) As Integer
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clSetKernelArg(ByVal kernel As IntPtr, ByVal argumentIndex As Integer, ByVal size As Integer, ByRef value As IntPtr) As Integer
        End Function

        <DllImport("OpenCL.dll")> _
        Public Shared Function clEnqueueTask(ByVal commandQueue As IntPtr, ByVal kernel As IntPtr, ByVal countOfEventsInWaitList As Integer, ByVal eventWaitList As IntPtr(), ByVal eventObject As IntPtr) As Integer
        End Function
        <DllImport("OpenCL.dll")> _
        Public Shared Function clEnqueueNDRangeKernel(ByVal commandQueue As IntPtr, ByVal kernel As IntPtr, ByVal workDimension As Integer, ByRef globalWorkOffset As MultiDimension, ByRef globalWorkSize As MultiDimension, ByRef localWorkSize As MultiDimension, _
         ByVal countOfEventsInWaitList As Integer, ByVal eventList As IntPtr(), ByVal eventObject As IntPtr) As Integer
        End Function
#End Region

    End Class

#Region "　Delegate　"
    Public Delegate Sub NotifyCallback(ByVal errorInfo As String, ByVal privateInfoSize As IntPtr, ByVal cb As Integer, ByVal userData As IntPtr)
    Public Delegate Sub NotifyProgramBuilt(ByVal program As IntPtr, ByVal userData As IntPtr)
#End Region


#Region "　列挙型　"
    ''' <summary>
    ''' プラットフォーム情報
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum PlatformInfo
        ''' <summary>プラットフォームのプロファイル</summary>
        Profile = &H900
        ''' <summary>プラットフォームのバージョン</summary>
        Version = &H901
        ''' <summary>プラットフォーム名</summary>
        Name = &H902
        ''' <summary>ベンダー情報</summary>
        Vendor = &H903
        ''' <summary>拡張情報</summary>
        Extensions = &H904
    End Enum

    ''' <summary>
    ''' デバイス種別
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum DeviceType As Long
        ''' <summary>初期設定として利用可能なOpenCLデバイス</summary>
        [Default] = (1 << 0)
        ''' <summary>OpenCLデバイスとして利用可能なホストのCPU</summary>
        Cpu = (1 << 1)
        ''' <summary>OpenCLデバイスとして利用可能なGPU</summary>
        Gpu = (1 << 2)
        ''' <summary>OpenCLデバイスとして利用可能なアクセラレータ(IBMのCellボード等)</summary>
        Accelerator = (1 << 3)
        ''' <summary>利用可能な全てのOpenCLデバイス</summary>
        All = &HFFFFFFFFUI
    End Enum

    ''' <summary>
    ''' デバイス情報
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum DeviceInfoString
        ''' <summary>デバイス名</summary>
        Name = &H102B
        ''' <summary>ベンダー情報</summary>
        Vendor = &H102C
        ''' <summary>デバイスのドライババージョン</summary>
        DriverVersion = &H102D
        ''' <summary>デバイスのプロファイル</summary>
        Profile = &H102E
        ''' <summary>デバイスのバージョン</summary>
        Version = &H102F
        ''' <summary>拡張情報</summary>
        Extensions = &H1030
    End Enum
    Public Enum MemoryFlags As Long
        ReadWrite = (1 << 0)
        [WriteOnly] = (1 << 1)
        [ReadOnly] = (1 << 2)
        UseHostMemory = (1 << 3)
        HostAccessible = (1 << 4)
        CopyHostMemory = (1 << 5)
    End Enum
#End Region

#Region "　構造体　"
    Public Structure MultiDimension
        Public X As Integer
        Public Y As Integer
        Public Z As Integer
        Public Dimension As Integer

        Public Sub New(ByVal x__1 As Integer)
            X = x__1
            Y = 0
            Z = 0
            Dimension = 1
        End Sub
    End Structure
#End Region

End Namespace